package com.brutal.minigames.gop;

import com.rsbuddy.script.methods.Environment;
import com.rsbuddy.script.methods.Game;

import org.rsbuddy.widgets.Lobby;

public class Login extends Thread {

	private final boolean isMember;

	public Login(boolean isMember) {
		this.isMember = isMember;
	}

	@Override
	public void run() {
		while (Game.getClientState() != 10) {
			try {
				Thread.sleep(50);
			} catch (Exception e) {
			}
			if (Game.getClientState() == 5 || Game.getClientState() == 7
					|| Game.getClientState() == 9) {
				Environment.disableRandom("Login");
				if (Game.getClientState() == 7) {
					if (Lobby.isOpen()) {
						if (Lobby.getSelectedTab() != Lobby.TAB_WORLDS) {
							Lobby.switchTab(Lobby.TAB_WORLDS);
						} else if (Lobby.getSelectedTab() == Lobby.TAB_WORLDS) {
							if (Lobby.WorldSelector.getSelectedWorld() != (isMember ? 60
									: 61)) {
								Lobby.WorldSelector.selectWorld(isMember ? 60
										: 61);
							} else if (!Lobby.isAlertOpen()) {
								Lobby.clickPlay();
							} else if (Lobby.getAlertText().contains("full")) {
								Lobby.closeAlert();
							}
						}
					}
				}
			} else {
				Environment.enableRandom("Login");
			}
		}
	}
}
